package hottargui.config;

import hottargui.framework.*;

public class AlphaMoveState implements TarguiState {

	Game gameContext;
	
	public AlphaMoveState(Game game)
	{
		gameContext = game;
	}
	
	public boolean buy(int count, Position deploy)
			throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}
	
	public State getState() {
		return State.move;
	}

	public boolean move(Position from, Position to, int count)
			throws IllegalActionException {
/*
		//TODO: Vi mangler lige noget til at validere om tr�kket mellem de to positioner er i orden.
		//TODO: S�fremt dette ikke er tilf�ldet skal der smides en exception
		//TODO: Vi skal lige holde �je med movestate st�r til det rigtige
		  
		boolean retval = true;
		Tile fromTile = gameContext.getTile(from);
		Tile toTile = gameContext.getTile(to);
		if(toTile.getOwnerColor() == PlayerColor.None)
		{
			toTile.changePlayerColor(fromTile.getOwnerColor());
			toTile.changeUnitCount(toTile.getUnitCount() + count);
			fromTile.changeUnitCount(fromTile.getUnitCount() - count);
		}
		else
		{
			//This is when a Tile is attacked
			if(fromTile.getOwnerColor() != toTile.getOwnerColor())
			{
				int res = fromTile.getUnitCount() - toTile.getUnitCount();
				if(res > 0) //The attacker wins the battle, res is the remaining camels
				{
					toTile.changePlayerColor( fromTile.getOwnerColor());
					toTile.changeUnitCount(res);
					fromTile.changeUnitCount(fromTile.getUnitCount() - count);
					  
				}
				else //Defender is the winner of the battle
				{
					fromTile.changeUnitCount(fromTile.getUnitCount() - count);
					toTile.changeUnitCount(-res);
				}
				  
			}
		}
*/
		boolean retval = false;
		
		Tile srcTile = gameContext.getTile(from);
		Tile targetTile = gameContext.getTile(to);
		
		// srcTile must be owned by player in turn
		if ( srcTile.getOwnerColor() == gameContext.getPlayerInTurn().getColor() )
		{
			Position target = targetTile.getPosition();
			Position src = srcTile.getPosition();
			
			int dCol = target.getColumn() - src.getColumn();
			int dRow = target.getRow() - src.getRow();
		
			// target must be adjacent
			if ( Math.abs(dCol) <= 1 && Math.abs(dRow) <= 1 )
			{
				// targetTile cannot be saltlake
				if ( targetTile.getType() != TileType.Saltlake )
				{
					if ( targetTile.getOwnerColor() != gameContext.getPlayerInTurn().getColor() )
					{
						// if someone is occupying the target we attack!
						if ( targetTile.getUnitCount() > 0 )
						{
							
							/*
							int dUnit = srcTile.getUnitCount() - targetTile.getUnitCount();
							srcTile.setUnitCount(srcTile.getUnitCount() - dUnit);
							targetTile.setUnitCount(targetTile.getUnitCount() - dUnit);
							*/
							
							int dUnit = srcTile.getUnitCount() - targetTile.getUnitCount();
							srcTile.setUnitCount(Math.max(dUnit, 0));
							targetTile.setUnitCount(Math.max(dUnit*-1, 0));
							
							count = Math.min(srcTile.getUnitCount(), count);
						}
					}
					
					// if count is 0 (perhaps after attack) no units are moved but move is successful
					if ( count == 0 )
					{
						retval = true;
					}
					else
					{
						// there must be at least count units available to move
						if ( srcTile.getUnitCount() >= count )
						{
							targetTile.setOwnerColor(srcTile.getOwnerColor());
							srcTile.setUnitCount(srcTile.getUnitCount() - count);
							targetTile.setUnitCount(targetTile.getUnitCount() + count);
							
							retval = true;
						}
					}
				}
			}
		}
		
		if ( retval )
		{
			// valid move sets state to buy
			this.gameContext.setState(this.gameContext.getFactory().getBuyState(this.gameContext));
		}
		
	    return retval;
	}

	public void newGame() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public void rollDie() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public PlayerColor turnCard() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public PlayerColor getWinner() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}
}
